//
//  HSFeedbackViewController.swift
//  HomeSchoolDemo
//
//  Created by Mac on 16/10/10.
//  Copyright © 2016年 chendong. All rights reserved.
//

import UIKit

class HSFeedbackViewController: UIViewController,UITextViewDelegate {
    
    var iswaite:Bool = false
    
    @IBOutlet weak var titleText: UITextField!
    
    @IBOutlet weak var contentText: UITextView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        contentText.delegate = self
        setupTopUI()
    }
    
    //顶部导航条
    func setupTopUI() {
        //模拟一个假导航栏
        let navVc = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64))
        navVc.backgroundColor = UIColor().colorWithHex(0x009ee7)
        view.addSubview(navVc)
        //返回按钮
        let back = UIButton(frame: CGRect(x: 8, y: 16, width: 70, height: 48))
        back.setImage(UIImage(named:"返回按钮"), for: .normal)
        back.addTarget(self, action: #selector(backBtnClicked), for: .touchUpInside)
        navVc.addSubview(back)
        //文字
        let label = UILabel(frame: CGRect(x: 60, y: 16, width: 150, height: 48))
        label.text = "关于我们"
        label.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        label.font = UIFont.systemFont(ofSize: 20)
        
        navVc.addSubview(label)
        
    }
    func backBtnClicked(){
        dismiss(animated: true, completion: nil)
    }
    //提交按钮回调方法
    @IBAction func SubmitClicked(_ sender: UIButton) {
        
        guard titleText.text != "" else {
            HSAlertView.bottomAlertView("请输入标题")
            return
        }
        guard contentText.text != "" else {
            HSAlertView.bottomAlertView("请输入内容")
            return
        }
        
        if !iswaite {
            HSAlertView.bottomAlertView("请输入内容")
            return
        }
        
        
        //拼接路径
        let url = "ZH-schoolname-S-submitJXOpinion"
        //参数
        let key = userInfo["key"] as! String
        let title = titleText.text!
        print(title)
        let content = contentText.text!
        print(content)
        let param = ["key":key,"title":title,"content":content]
        //上传意见
        HSNetworkTools.shared.downloadData(url: url, parameters: param) {[weak self] (data, error) in
            //print(data,error)
            guard let data = data as? NSDictionary else {
                HSAlertView.bottomAlertView("网络异常")
                return
            }
            if data["result"] as! Int == 1{
                HSAlertView.bottomAlertView("提交失败,请重试")
                return
            }else if data["result"] as! Int == 0 {
                HSAlertView.bottomAlertView("提交成功，返回上级页面")
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5, execute: {
                    self?.dismiss(animated: true, completion: nil)
                })
            }
        }
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        if iswaite == false {
            textView.text = nil
            textView.textColor = UIColor.black
            iswaite = true
        }
    }
}

extension HSFeedbackViewController{
    //回收键盘
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
}



